package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtEproductDao extEproductDao;
    @Autowired
    private ContractDao contractDao;
    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
       String[] contractIds = export.getContractIds().split(","); // 9c57232e-62e0-4483-a474-9dd9d8a35c8c,1b393874-3f23-4d04-ab53-c2c3607adca0
//        把数组转成集合
        List<String> contractIdList = Arrays.asList(contractIds);

//        查询这些购销合同下的货物 select * from co_contract_product where contract_id in ();

        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdIn(contractIdList);
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);
//        把购销合同货物转成报运单货物
        for (ContractProduct contractProduct : contractProductList) {
            ExportProduct exportProduct = new ExportProduct();
            org.springframework.beans.BeanUtils.copyProperties(contractProduct,exportProduct);//            属性名和数据类型都要一致才会copy
//            还少一个exportId需要赋值
            exportProduct.setExportId(export.getId());
            exportProductDao.insertSelective(exportProduct);

//            直接可以获取到当前购销合同货物下的附件
            List<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
//        把购销合同附件转成报运单附件
            for (ExtCproduct extCproduct : extCproducts) {
                ExtEproduct extEproduct = new ExtEproduct();
                org.springframework.beans.BeanUtils.copyProperties(extCproduct,extEproduct);
//                还需要手动赋值的有：exportProductId和exportId
                extEproduct.setExportId(export.getId());
                extEproduct.setExportProductId(exportProduct.getId());

                extEproductDao.insertSelective(extEproduct);
            }

        }

//        查询多个购销合同
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andIdIn(contractIdList);
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        StringBuffer customerContract  = new StringBuffer("");

        Integer totalProNum = 0 ;
        Integer totalExtNum = 0 ;
        for (Contract contract : contractList) {
            customerContract.append(contract.getCustomName()).append(" ");
            totalProNum+=contract.getProNum();
            totalExtNum+=contract.getExtNum();

            contract.setState(2);
            contractDao.updateByPrimaryKeySelective(contract);
        }

        export.setCustomerContract(customerContract.toString()); //多个购销合同客户名称使用空格拼接
        export.setProNum(totalProNum); //多个购销合同下的货物数的总和
        export.setExtNum(totalExtNum);//多个购销合同下的附件数的总和

        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
//        修改报运单、报运单下的货物
        exportDao.updateByPrimaryKeySelective(export);
        List<ExportProduct> exportProducts = export.getExportProducts();
        if(!CollectionUtils.isEmpty(exportProducts)){
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public void delete(String id) {
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
//        更新两个表
//        export   状态  备注
        Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        exportDao.updateByPrimaryKeySelective(export);
//
//        exportProduct 税
        Set<ExportProductResult> products = exportResult.getProducts();
        if(!CollectionUtils.isEmpty(products)){
            for (ExportProductResult product : products) {
                ExportProduct exportProduct = new ExportProduct();
                exportProduct.setId(product.getExportProductId());
                exportProduct.setTax(product.getTax());
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public List<Export> findByState(Long state) {
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andStateEqualTo(state);
        return exportDao.selectByExample(exportExample);
    }
}
